package com.qianda.qdminiapp.service.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qianda.qdminiapp.common.constant.CommonConstant;
import com.qianda.qdminiapp.common.exception.ExceptionCode;
import com.qianda.qdminiapp.common.exception.SimpleException;
import com.qianda.qdminiapp.common.rabbitmq.MsgProducer;
import com.qianda.qdminiapp.common.util.DateUtils;
import com.qianda.qdminiapp.config.WxpayProperties;
import com.qianda.qdminiapp.controller.payment.WxPayNotify;
import com.qianda.qdminiapp.dao.account.QqdAccountTradeDao;
import com.qianda.qdminiapp.dao.device.DeviceDao;
import com.qianda.qdminiapp.dao.device.QqdDeviceDao;
import com.qianda.qdminiapp.dao.device.QqdStoreDao;
import com.qianda.qdminiapp.dao.order.OrderDao;
import com.qianda.qdminiapp.dao.order.QqdOrderInfoDao;
import com.qianda.qdminiapp.dao.order.QqdOrderTowelDao;
import com.qianda.qdminiapp.dao.personmanage.QqdCompanyDao;
import com.qianda.qdminiapp.dao.user.QqdUserDao;
import com.qianda.qdminiapp.dao.user.UserDao;
import com.qianda.qdminiapp.domain.dto.OrderAndPayCheckDto;
import com.qianda.qdminiapp.domain.dto.OrderAndPayDto;
import com.qianda.qdminiapp.domain.model.account.QqdAccountTrade;
import com.qianda.qdminiapp.domain.model.order.Order;
import com.qianda.qdminiapp.domain.model.order.QqdOrderInfo;
import com.qianda.qdminiapp.domain.model.order.QqdOrderTowel;
import com.qianda.qdminiapp.domain.model.user.QqdUser;
import com.qianda.qdminiapp.domain.model.user.User;
import com.qianda.qdminiapp.service.CommondManager;
import com.qianda.qdminiapp.service.pay.WxPayment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 微信支付
 * @ClassName WxPaymentImpl
 * @Author cw
 * @Date 2019/8/7 13:40
 * @Version 1.0
 */
public class WxPaymentImpl extends AbstractOrderAndPay {

    private static Logger LOGGER = LoggerFactory.getLogger(WxPayNotify.class);

    private WxpayProperties wxpayProperties;

    public WxPaymentImpl(QqdUserDao userDao, QqdOrderInfoDao orderInfoDao, QqdOrderTowelDao qqdOrderTowelDao,
                         CommondManager commondManager, QqdDeviceDao deviceDao,
                         WxpayProperties wxpayProperties, QqdStoreDao qqdStoreDao, QqdAccountTradeDao qqdAccountTradeDao,
                         MsgProducer msgProducer, QqdCompanyDao qqdCompanyDao) {
        super(userDao, orderInfoDao,qqdOrderTowelDao, commondManager, deviceDao,qqdStoreDao,qqdAccountTradeDao,msgProducer,qqdCompanyDao);
        this.wxpayProperties =wxpayProperties;
    }

    /*
     * 微信支付发起前校验方法
     * @Description
     * @Author cw
     * @Date 14:12 2019/8/7
     * @Param []
     * @return boolean
     **/
    @Override
    public boolean checkStatus(OrderAndPayDto orderAndPayDto) {
       // throw new SimpleException(ExceptionCode.EX_SYSTEM1);
        return true;
    }

    /*
     *  微信支付方法
     * @Description
     * @Author cw
     * @Date 14:13 2019/8/7
     * @Param [order]
     * @return void
     **/
    @Override
    public Map<String,Object> appPay(QqdOrderInfo qqdOrderInfo) {
        LOGGER.info("开始进入微信支付");
        QqdUser user = userDao.selectById(qqdOrderInfo.getOrderUserId());

        Map<String, Object> wxPay = (new WxPayment()).wxPay(user.getUserOpenId(),qqdOrderInfo,wxpayProperties);

        if(qqdOrderInfo.getOrderType() == CommonConstant.QqdOrderType.QQD_ORDER_TYPE_BUY_TOWEL){
            QueryWrapper<QqdOrderTowel> qqdOrderTowelQueryWrapper = new QueryWrapper<>();
            qqdOrderTowelQueryWrapper.eq("order_id",qqdOrderInfo.getId()).eq("status",CommonConstant.Status.STATUS_NORMAL);
            QqdOrderTowel order = qqdOrderTowelDao.selectOne(qqdOrderTowelQueryWrapper);
            order.setOrderPayTime(LocalDateTime.now());
            order.setOrderPayType(CommonConstant.PayType.PAY_TYPE_WX);
            order.setOrderPayStatus(CommonConstant.PayStatus.PAY_STATUS_WAIT);
            order.setOrderStatus(CommonConstant.OrderOutTowelStatus.ORDER_OUT_TOWEL_STATUS_WAIT);
            order.setUpdateTime(LocalDateTime.now());
            qqdOrderTowelDao.updateById(order);
        }
        return wxPay;
    }

    /*
     * 微信退款
     * @Description
     * @Author cw
     * @Date 11:49 2019/8/9
     * @Param [order]
     * @return void
     **/
    @Override
    public void refundPay(QqdOrderTowel order) {
        LOGGER.info("开始发起退款");
        QqdOrderInfo qqdOrderInfo = orderInfoDao.selectById(order.getOrderId());
        QqdOrderTowel qqdOrderTowel = qqdOrderTowelDao.selectById(order.getId());
        if(qqdOrderTowel.getOrderPayStatus() == CommonConstant.PayStatus.PAY_STATUS_REFUND ){
            return;
        }

        //微信退款
        (new WxPayment()).WxRefund(qqdOrderTowel,qqdOrderInfo,wxpayProperties);

        //订单支付状态改为已退款
        qqdOrderTowel.setOrderPayStatus(CommonConstant.PayStatus.PAY_STATUS_REFUND);
        qqdOrderTowelDao.updateById(qqdOrderTowel);
    }
}
